/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package privileges.bean;

import data_bases.DAO.DatabaseDAO;
import data_bases.beans.TableBean;
import data_bases.business.DataBaseBusiness;
import data_bases.business.Schema;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import privileges.business.UserBusiness;

/**
 *
 * @author Javier
 */
@ManagedBean
@SessionScoped
public class ColumnPrivilegesBean implements Serializable{
    
    private String user;
    private String host;
    private List<String> selectedOptions;
    private List<String> selectedOptionsTables;
    private List<String> selectedOptionsSelect;
    private List<String> selectedOptionsInsert;
    private List<String> selectedOptionsUpdate;
    private List<String> selectedOptionsReferences;
    private List<Schema> schemas;
    private List<String> tables;
    private String selectedSchema;
    private String selectedTable;
    private boolean checkSel = false;
    private boolean checkIns = false;
    private boolean checkUpd = false;
    private boolean checkRef = false;
    private AdvancedPrivilegesBean apb ;

    public ColumnPrivilegesBean() {
        ///FORMA 1
        apb = (AdvancedPrivilegesBean) FacesContext.getCurrentInstance()
                   .getExternalContext().getSessionMap().get("advancedPrivilegesBean");
        user = apb.nameUser;
        host = apb.host;
        System.out.println("llama al constructor");
        try {
            schemas = new DataBaseBusiness(new DatabaseDAO().viewDatabases()).getSchemas();
        } catch (SQLException ex) {
            Logger.getLogger(TableBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isCheckSel() {
        return checkSel;
    }

    public void setCheckSel(boolean checkSel) {
        this.checkSel = checkSel;
    }

    public boolean isCheckIns() {
        return checkIns;
    }

    public void setCheckIns(boolean checkIns) {
        this.checkIns = checkIns;
    }

    public boolean isCheckUpd() {
        return checkUpd;
    }

    public void setCheckUpd(boolean checkUpd) {
        this.checkUpd = checkUpd;
    }

    public boolean isCheckRef() {
        return checkRef;
    }

    public void setCheckRef(boolean checkRef) {
        this.checkRef = checkRef;
    }
    
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public List<String> getSelectedOptions() {
        return selectedOptions;
    }

    public void setSelectedOptions(List<String> selectedOptions) {
        this.selectedOptions = selectedOptions;
    }
    
    public List<Schema> getSchemas() {
        return schemas;
    }

    public void setSchemas(List<Schema> schemas) {
        this.schemas = schemas;
    }

    public List<String> getTables() {
        return tables;
    }

    public void setTables(List<String> tables) {
        this.tables = tables;
    }

    public String getSelectedSchema() {
        return selectedSchema;
    }

    public void setSelectedSchema(String selectedSchema) {
        this.selectedSchema = selectedSchema;
    }

    public List<String> getSelectedOptionsTables() {
        return selectedOptionsTables;
    }

    public void setSelectedOptionsTables(List<String> selectedOptionsTables) {
        this.selectedOptionsTables = selectedOptionsTables;
    }

    public List<String> getSelectedOptionsSelect() {
        return selectedOptionsSelect;
    }

    public void setSelectedOptionsSelect(List<String> selectedOptionsSelect) {
        this.selectedOptionsSelect = selectedOptionsSelect;
    }

    public List<String> getSelectedOptionsInsert() {
        return selectedOptionsInsert;
    }

    public void setSelectedOptionsInsert(List<String> selectedOptionsInsert) {
        this.selectedOptionsInsert = selectedOptionsInsert;
    }

    public List<String> getSelectedOptionsUpdate() {
        return selectedOptionsUpdate;
    }

    public void setSelectedOptionsUpdate(List<String> selectedOptionsUpdate) {
        this.selectedOptionsUpdate = selectedOptionsUpdate;
    }

    public List<String> getSelectedOptionsReferences() {
        return selectedOptionsReferences;
    }

    public void setSelectedOptionsReferences(List<String> selectedOptionsReferences) {
        this.selectedOptionsReferences = selectedOptionsReferences;
    }

    public String getSelectedTable() {
        return selectedTable;
    }

    public void setSelectedTable(String selectedTable) {
        this.selectedTable = selectedTable;
    }
    
    //Listener de cuando ocurre un cambio en el menu de Schemas
    public void reloadColumns(){
        //Limpia el menu de tablas
        selectedTable = "";
        //Limpia las tablas de los privilegios por columnas
        if(!(selectedOptions == null))
            selectedOptions.clear();
        //Limpia la tabla de privilegios por tabla
        if(!(selectedOptionsTables == null))
            selectedOptionsTables.clear();
        FacesContext context = FacesContext.getCurrentInstance();
        //Limpia los checkbox de ningún permiso
        checkIns = false;
        checkRef = false;
        checkSel = false;
        checkUpd = false;
        try {   
         //Una vez verifica que haya un schema seleccionado carga las tablas del mismo para cargar el segundo menu.
            if(!(selectedSchema ==null))
                if(!selectedSchema.equals(""))
                    tables = new UserBusiness().getTablesFromDb(selectedSchema);
        } catch (SQLException ex) {
            context.addMessage(null,new FacesMessage("Error", "No se cargaron los Esquemas"));
        }
    }
    //Listener de cuando ocurre un cambio en el menu de schemas
    public void reloadTables(){
        
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            //Se colocan los permisos bloqueados
            checkIns = false;
            checkRef = false;
            checkSel = false;
            checkUpd = false;
            //Se carga la lista de privilegios por columnas y por tablas para mostrarlos en la interfaz
            selectedOptions = new UserBusiness().getColumnNames(selectedSchema,selectedTable);
            List<List<String> > listPrivileges = new UserBusiness().getColumnsSelect(user,host,selectedSchema,selectedTable);
            selectedOptionsSelect = listPrivileges.get(0);
            selectedOptionsInsert = listPrivileges.get(1);
            selectedOptionsUpdate = listPrivileges.get(2);
            selectedOptionsReferences = listPrivileges.get(3);           
            selectedOptionsTables = new UserBusiness().getPrivilegesOnTable(user,host,selectedSchema,selectedTable);
        } catch (SQLException ex) {
            context.addMessage(null,new FacesMessage("Error", ex.getLocalizedMessage()));
        }
    }
    public void changeSel(){
        selectedOptionsSelect.clear();
    }
    public  void changeIns(){
        selectedOptionsInsert.clear();
    }
    public void changeUpd(){
        selectedOptionsUpdate.clear();
    }
    public void changeRef(){
        selectedOptionsReferences.clear();
    }
    
    public void doPrivileges(){
        FacesContext context = FacesContext.getCurrentInstance();
        String message = new UserBusiness().setPrivilegesOnTable(user,host,selectedSchema
                ,selectedTable,selectedOptionsTables,selectedOptionsSelect
                ,selectedOptionsInsert,selectedOptionsUpdate,selectedOptionsReferences);
        context.addMessage(null, new FacesMessage("Mensaje", message));   
    }
}
